﻿Imports System.ComponentModel
Imports System.Linq.Expressions

Public Class Attendance
	Inherits AutomaticNotfiyPropertyChanged
	Implements IDisposable

	Dim workingAttendanceData As AttendanceData
	Dim db As New CongregationDataContainerLocal

	Public Shared ReadOnly Property Months As BindingList(Of DateTime)
		Get
			Using db As New CongregationDataContainerLocal
				Dim returnList As New BindingList(Of DateTime)
				Dim items = (From myDate In db.PublisherCardDatas
				   Select LocalMonth = myDate.Month
				  Distinct).ToList

				items.Add(Now.FirstOfTheMonth)

				Dim filteredItems = From myDate In items
				  Select FirstDayOfMonth = myDate.FirstOfTheMonth
				  Order By FirstDayOfMonth Descending
				  Distinct

				For Each item In filteredItems 'db.PublisherCardDatas.OrderByDescending(Function(x) x.Month).Select(Function(x) x.Month).Select(Function(x) x.Month.firstofthemonth).Distinct.ToList()
					returnList.Add(item)
				Next
				Return returnList
			End Using
		End Get
	End Property

	Public Property Month As DateTime
		Get
			Return workingAttendanceData.ReportMonth
		End Get
		Set(value As DateTime)
			'we have to create a new record if we change this
			ChangeWorkingAttendanceWeek(value, CountIdentifier)
		End Set
	End Property

	Public Shared ReadOnly Property CountIdentifiers As BindingList(Of String)
		Get
			Using db As New CongregationDataContainerLocal
				Dim returnList As New BindingList(Of String)
				For Each item In db.AttendanceDatas.OrderBy(Function(x) x.MeetingLocation).Select(Function(x) x.MeetingLocation).ToList
					returnList.Add(item)
				Next
				'returnList.Add("[Default]")
				Return returnList
			End Using
		End Get
	End Property

	Public Property CountIdentifier As String
		Get
			'If workingAttendanceData.MeetingLocation Is Nothing Then
			'	CountIdentifier = "Congregation Total"
			'	Return workingAttendanceData.MeetingLocation
			'Else
			Return workingAttendanceData.MeetingLocation
			'End If
		End Get
		Set(value As String)
			'we have to create a new record if we change this
			ChangeWorkingAttendanceWeek(Month, value)
		End Set
	End Property

	Public Property CBSTMSSMWeek1 As Integer
		Get
			Return workingAttendanceData.Week1CBSTMSSMAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week1CBSTMSSMAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.CBSTMSSMAverage)
		End Set
	End Property

	Public Property CBSTMSSMWeek2 As Integer
		Get
			Return workingAttendanceData.Week2CBSTMSSMAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week2CBSTMSSMAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.CBSTMSSMAverage)
		End Set
	End Property

	Public Property CBSTMSSMWeek3 As Integer
		Get
			Return workingAttendanceData.Week3CBSTMSSMAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week3CBSTMSSMAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.CBSTMSSMAverage)
		End Set
	End Property

	Public Property CBSTMSSMWeek4 As Integer
		Get
			Return workingAttendanceData.Week4CBSTMSSMAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week4CBSTMSSMAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.CBSTMSSMAverage)
		End Set
	End Property

	Public Property CBSTMSSMWeek5 As Integer
		Get
			Return workingAttendanceData.Week5CBSTMSSMAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week5CBSTMSSMAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.CBSTMSSMAverage)
		End Set
	End Property

	Public ReadOnly Property CBSTMSSMCount As Integer
		Get
			Dim numOfWeeks As Integer
			Dim countList As New List(Of Integer)

			countList.Add(CBSTMSSMWeek1)
			countList.Add(CBSTMSSMWeek2)
			countList.Add(CBSTMSSMWeek3)
			countList.Add(CBSTMSSMWeek4)
			countList.Add(CBSTMSSMWeek5)

			numOfWeeks = countList.Where(Function(x) x > 0).Count

			Return numOfWeeks
		End Get
	End Property

	Public ReadOnly Property CBSTMSSMTotal As Integer
		Get
			Dim countList As New List(Of Integer)

			countList.Add(CBSTMSSMWeek1)
			countList.Add(CBSTMSSMWeek2)
			countList.Add(CBSTMSSMWeek3)
			countList.Add(CBSTMSSMWeek4)
			countList.Add(CBSTMSSMWeek5)

			Return countList.Sum
		End Get
	End Property

	Public ReadOnly Property CBSTMSSMAverage As Decimal
		Get
			Return If(CBSTMSSMCount = 0, 0, CBSTMSSMTotal / CBSTMSSMCount)
		End Get
	End Property

	Public Property PTWTWeek1 As Integer
		Get
			Return workingAttendanceData.Week1PTWTAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week1PTWTAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.PTWTAverage)
		End Set
	End Property

	Public Property PTWTWeek2 As Integer
		Get
			Return workingAttendanceData.Week2PTWTAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week2PTWTAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.PTWTAverage)
		End Set
	End Property

	Public Property PTWTWeek3 As Integer
		Get
			Return workingAttendanceData.Week3PTWTAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week3PTWTAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.PTWTAverage)
		End Set
	End Property

	Public Property PTWTWeek4 As Integer
		Get
			Return workingAttendanceData.Week4PTWTAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week4PTWTAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.PTWTAverage)
		End Set
	End Property

	Public Property PTWTWeek5 As Integer
		Get
			Return workingAttendanceData.Week5PTWTAttendance
		End Get
		Set(value As Integer)
			workingAttendanceData.Week5PTWTAttendance = value
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.PTWTAverage)
		End Set
	End Property

	Public ReadOnly Property PTWTCount As Integer
		Get
			Dim numOfWeeks As Integer
			Dim countList As New List(Of Integer)

			countList.Add(PTWTWeek1)
			countList.Add(PTWTWeek2)
			countList.Add(PTWTWeek3)
			countList.Add(PTWTWeek4)
			countList.Add(PTWTWeek5)

			numOfWeeks = countList.Where(Function(x) x > 0).Count

			Return numOfWeeks
		End Get
	End Property

	Public ReadOnly Property PTWTTotal As Integer
		Get
			Dim countList As New List(Of Integer)

			countList.Add(PTWTWeek1)
			countList.Add(PTWTWeek2)
			countList.Add(PTWTWeek3)
			countList.Add(PTWTWeek4)
			countList.Add(PTWTWeek5)

			Return countList.Sum
		End Get
	End Property

	Public ReadOnly Property PTWTAverage As Decimal
		Get
			Return If(PTWTCount = 0, 0, PTWTTotal / PTWTCount)
		End Get
	End Property

	Public Sub New(ReportMonth As DateTime, Optional MeetingLocation As String = Nothing)
		ChangeWorkingAttendanceWeek(ReportMonth, MeetingLocation)
	End Sub

	Private Sub ChangeWorkingAttendanceWeek(ReportMonth As DateTime, Optional MeetingLocation As String = Nothing)

		'workingAttendanceData = db.AttendanceDatas.SingleOrDefault(Function(x) x.ReportMonth = ReportMonth AndAlso x.MeetingLocation = MeetingLocation)
		workingAttendanceData = db.AttendanceDatas.SingleOrDefault(Function(x) x.ReportMonth = ReportMonth)

		If workingAttendanceData Is Nothing Then
			'we have a new month here
			workingAttendanceData = New AttendanceData
			workingAttendanceData.ReportMonth = ReportMonth.FirstOfTheMonth
			If MeetingLocation <> "" Then
				workingAttendanceData.MeetingLocation = MeetingLocation
			End If
			db.AttendanceDatas.AddObject(workingAttendanceData)
			db.SaveChanges()
		End If

	End Sub

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
